<?php
namespace AuctionCore\AuctionOrder;
use AuctionCore\System\AuctionException;
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//


/**
* 场次
* @author       爱民
*/
class Scene {
    
    /**
    * 新建场次
    * @param    int $sence_info    场信息[emp_id:操作员工id,starttime:开始时间，endtime:结束时间,name:标题] ,
    *
    * @return   int 返回场次id号
    */
    public static function create($scene_info) {
        foreach (array('name','starttime') as $value) {
            if(!$scene_info[$value])
               AuctionException::make_throw('700001'); 
        }

        $scene_info['createtime'] = date("Y-m-d H:i:s",time());;
        $scene = M('order_scene');
        $insertId = $scene->add($scene_info);
        return $insertId;
    }
    /*
     * 获取有效场次
     * */
    public function get_scenes(){
        return M('order_scene')->where("status='1' and starttime>'".date('Y-m-d H:i:s')."'")->order("scene_id desc")->select();
    }
    /*
     * 获取场次信息
     * */
    public function detail($scene_id){
        if(!intval($scene_id)){
            return false;
        }
        return M('order_scene')->find($scene_id);
    }
    /**
    * 更新场次信息
    * @param    int $sence_info    场信息[开始时间，结束时间,标题,描述] ,
    *                             返回成功与否
    * @return   boolean
    */
    public static function change($scene_id,$scene_info) {
            //只能编辑场次状态为1的（即未开始拍卖的）
            $scene = self::detail($scene_id);
            if(!$scene){
                AuctionException::make_throw('700028');
            }
            if($scene['status']=='2'){
                AuctionException::make_throw('700029');
            }
            if($scene['status']=='3'){
                AuctionException::make_throw('700030');
            }

            $ok = M('order_scene')->where("scene_id='".$scene_id."'")->save($scene_info);
            return $ok;
    }

    /**
     * 得新计算场次内非定时单的开拍时间
     * @param $scene_id
     * @return bool
     */
    public function re_compute_starttime($scene_id){
            $res = M('order_scene')->alias('os')->field('os.starttime,o.order_id,o.act_elapsed_time,o.est_elapsed_time')->join(C('DB_PREFIX')."order o ON o.scene_id=os.scene_id")
                ->where("os.scene_id='".$scene_id."' AND o.is_timing_order=0 AND o.rank>0 AND o.status NOT IN(2,6,12)")->order("o.rank")
                ->select();
            $arr = array();
            $timestamp = 0;
            foreach($res as $v){
            $timestamp += $v['act_elapsed_time']>$v['est_elapsed_time']?$v['act_elapsed_time']:$v['est_elapsed_time'];
                $arr[] = $timestamp;
            }
            //
            $rs = array();
            foreach($res as $key=>$val){
                $data = array();
                $data['bidding_start_time'] = date("Y-m-d H:i:s",strtotime($val['starttime'])+$arr[$key-1]);
                $rs[]=M('order')->where("order_id='".$val['order_id']."'")->save($data);
            }
            return !in_array(false,$rs) ?true:false;
    }

    public function check_starttime($scene_id,$starttime){
            $res = M('order')->where("scene_id='".$scene_id."' and is_timing_order=1 and rank>0 and status not in(2,6,12)")->order('bidding_start_time')->find();
            if($res){
                if(strtotime($res['bidding_start_time'])<strtotime($starttime)){
                   return false;
                }
            }
            return true;
    }
    public static function remove($scene_id){
        $ok = M('order_scene')->where('scene_id=\''.$scene_id.'\'')->delete();
        return $ok;
    }

    /**
     * 获取最近一场的场次信息
     * @return mixed
     */
    public function get_last_scene($city=''){
        $map = array();
        $map['o.status'] = array('eq',3);
        //今明两天的
        $map['os.starttime'] = array(array('gt', date('Y-m-d 00:00:00')), array('lt', date('Y-m-d 23:59:59', strtotime('+1 day'))));
        $map['os.status'] = array('in', '1,2');//场次状态 -1禁用 1待拍 2拍卖中 3 完成
        if($city){
            $map['os.city'] = array('eq',$city);
        }
        //状态为1,2的
        $res = M('order_scene')->alias('os')->join('LEFT JOIN '.C('DB_PREFIX').'order o ON o.scene_id=os.scene_id')->where($map)->order('os.starttime')->find();
        return $res;
    }
}

?>